using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using RadinfMagazynier3.Forms;

namespace RadinfMagazynier3.Tools
{
    class RadinfTools
    {
        #region CONVERSIONS
        public static int ConvertToInt(object obj)
        {
            bool fail = false;
            int result = -1;

            try
            {
                if (obj != null && obj != DBNull.Value)
                    fail = !(int.TryParse(obj.ToString(), out result));
                else
                    fail = true;
            }
            catch (Exception exc)
            {
                ShowError("RadinfTools.ConvertToInt()", exc.Message);
                fail = true;
            }
            finally
            {
                if (fail)
                    result = -1;
            }

            return result;
        }

        public static long ConvertToLong(object obj)
        {
            bool fail = false;
            long result = -1;
            try
            {
                if (obj != null && obj != DBNull.Value)
                    fail = !(long.TryParse(obj.ToString(), out result));
                else
                    fail = true;
            }
            catch(Exception exc)
            {
                ShowError("RadinfTools.ConvertToLong()", exc.Message);
                fail = true;
            }
            finally
            {
                if (fail)
                    result = -1;
            }
            return result;
        }

        public static double ConvertToDouble(object obj)
        {
            bool fail = false;
            double result = -1;

            try
            {
                if (obj != null && obj != DBNull.Value)
                    fail = !(double.TryParse(obj.ToString(), out result));
                else
                    fail = true;
            }
            catch (Exception exc)
            {
                ShowError("RadinfTools.ConvertToDouble()", exc.Message);
                fail = true;
            }
            finally
            {
                if (fail)
                    result = -1;
            }

            return result;
        }

        public static string ConvertToString(object obj)
        {
            bool fail = false;

            string result = string.Empty;
            try
            {
                if (null != obj)
                    result = obj.ToString();
                else
                    fail = true;
            }
            catch (Exception exc)
            {
                ShowError("RadinfTools.ConvertToString()", exc.Message);
                fail = true;
            }
            finally
            {
                if (fail)
                    result = string.Empty;
            }

            return result;
        }
        #endregion

        public static string EscapeSQL(string text)
        {
            return "'" + text + "'";
        }
        
        public static void ShowError(string from, string error)
        {
            MessageBox.Show(from + " reports an error: " + error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            MainForm.Logs.Add(from + " reports an error: " + error);
        }

        public static List<string> GetTablesWithContstraintError(DataSet ds)
        {
            List<string> tables = new List<string>();
            if (ds != null)
            {
                foreach (DataTable dt in ds.Tables)
                {
                    foreach (DataRow dr in dt.GetErrors())
                    {
                        if (null != tables)
                            tables.Add(dr.Table.TableName);
                    }
                }
            }
            return tables;
        }

        public enum KAKA { DE, MO, NA };
    }
}
